// pages/confirmorder/confirmorder.js
let arrs = [];

Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderlist: [],
    choiceaddres: {}, //确认订单页进入地址页进行选择地址
    buynow: 0, //0代表从购物车界面提交订单，1代表点击立即购买提交订单
    addressList: [],
    nowbuydetail: {}, //立即购买时商品的信息
    pricetotal: 0, //商品折扣前总金额
    pricedis: 0.00, //商品优惠金额
    numsum: 0, //提交订单商品总件数
    pricesum: 0, //折扣后商品总价格
    arrst: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    let that = this
    that.setData({
      buynow: options.buynow
    })
    if (options.buynow == 1) {
      let arr = []
      arrs = wx.getStorageSync('orderlists') || []
      console.log(arrs)
      // wx.getStorage({
      //   key: 'orderlists',
      //   success: function(res) {
      //     console.log(res.data)
      //     arr = res.data
      //   },
      //   fail: function(res) {
      //   },
      //   complete: function(res) {},
      // })
      console.log(JSON.parse(options.detail))
      let list = JSON.parse(options.detail)
      wx.setStorage({
        key: 'nowbuydetail',
        data: list,
      })
      that.setData({
        nowbuydetail: list
      })
      arr.push(list)
      let arrnowbuy = []
      arrnowbuy.push(list)
      arrs.push(arrnowbuy)
      console.log(arrs)
      if (list.discount == '') {
        that.setData({
          buynow: options.buynow,
          orderlist: arr,
          pricedis: 0.00,
          numsum: list.num,
          pricesum: list.num * list.price
        })
      } else {
        that.setData({
          buynow: options.buynow,
          orderlist: arr,
          pricedis: list.price * (1 - parseFloat(list.discount / 10)) * list.num,
          numsum: list.num,
          pricesum: list.num * list.price
        })
      }
    } else if (options.buynow == 0) {
      arrs = wx.getStorageSync('orderlists') || []
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let that = this
    let numarr = [] //商品总数量
    let pricearr1 = [] //商品折扣总金额
    let pricearr2 = [] //商品折扣后总价格
    let pricearr3 = [] //商品折扣前总价格
    if (that.data.buynow == 0) {
      wx.getStorage({
        key: 'carlists',
        success: function(res) {
          that.setData({
            orderlist: res.data
          })
          res.data.forEach((item, index) => {
            numarr.push(item.num)
            if (item.discount == '') {
              pricearr1.push(0.00)
            } else {
              pricearr1.push(item.price * (1 - parseFloat(item.discount / 10)) * item.num)
            }
            pricearr2.push(item.price * item.num)
          })
          if (pricearr1.length == 0) {
            that.setData({
              pricedis: 0.00,
            })
          } else {
            that.setData({
              pricedis: pricearr1.reduce((item, index) => {
                return item + index
              }),
            })
          }
          if (numarr.length == 0) {
            that.setData({
              numsum: 0,
            })
          } else {
            that.setData({
              numsum: numarr.reduce((item, index) => {
                return item + index
              }),
            })
          }
          if (pricearr2.length == 0) {
            that.setData({
              pricedis: 0.00,
            })
          } else {
            that.setData({
              pricesum: pricearr2.reduce((item, index) => {
                return item + index
              }),
            })
          }
        },
      })
    }
    if (that.data.buynow == 1) {
      console.log(arrs)
    }
    wx.getStorage({
      key: 'addresslist',
      success: function(res) {
        that.setData({
          addressList: res.data,
          choiceaddres: res.data[0]
        })
      },
    })
    wx.getStorage({
      key: 'choiceaddres',
      success: function(res) {
        that.setData({
          choiceaddres: res.data
        })
      },
      fail: function() {
        that.setData({
          choiceaddres: that.data.addressList[0]
        })
      }
    })
  },


  //选择地址
  chooseaddress: function() {
    let that = this
    console.log(that.data.nowbuydetail)
    wx.navigateTo({
      url: '../../pages/addressList/addressList?chooseaddr=0&buynow=' + that.data.buynow + '&detail=' + JSON.stringify(that.data.nowbuydetail),
    })
  },

  //点击去支付
  confirmorder: function() {
    let that = this
    console.log(that.data.buynow)
    wx.getStorage({
      key: 'addresslist',
      success: function(res) {
        if (res.data.length == 0) {
          wx.showModal({
            title: '提示',
            confirmText: '确定',
            cancelText: '取消',
            content: '请先设置地址',
            success: function(res) {
              if (res.confirm) {
                wx.redirectTo({
                  url: '../../pages/addressList/addressList?chooseaddr=0&buynow=' + that.data.buynow + '&detail=' + JSON.stringify(that.data.nowbuydetail)
                })
              } else if (res.cancel) {
                return;
              }
            }
          })
        } else {
          if (that.data.buynow == 0) {
            wx.getStorage({
              key: 'carlists',
              success: function(res) {
                console.log(res.data)
                console.log(that.data.addressList)
                res.data[0].addrname = that.data.choiceaddres.name
                res.data[0].tel = that.data.choiceaddres.tel
                res.data[0].city = that.data.choiceaddres.city
                res.data[0].detail = that.data.choiceaddres.detailaddr
                console.log(res.data)
                wx.showModal({
                  title: '确认支付',
                  content: '请认真核对订单信息',
                  confirmText: '确认支付',
                  cancelText: '取消支付',
                  success(re) {
                    if (re.confirm) {
                      console.log('用户点击确定')
                      res.data[0].payinfo = 1
                      wx.showModal({
                        content: '支付成功',
                        confirmText: '查看订单',
                        confirmColor: '#e50329',
                        cancelText: '返回首页',
                        success(res) {
                          if (res.confirm) {
                            wx.navigateTo({
                              url: '../../pages/order/order',
                            })
                          } else if (res.cancel) {
                            wx.switchTab({
                              url: '../../pages/home/home',
                            })
                          }
                        }
                      })
                    } else if (re.cancel) {
                      res.data[0].payinfo = 0
                      wx.showModal({
                        content: '支付失败',
                        confirmText: '查看订单',
                        confirmColor: '#e50329',
                        cancelText: '返回首页',
                        success(r) {
                          if (r.confirm) {
                            wx.navigateTo({
                              url: '../../pages/order/order',
                            })
                          } else if (r.cancel) {
                            wx.switchTab({
                              url: '../../pages/home/home',
                            })
                          }
                        }
                      })
                    }
                    arrs.push(res.data)
                    wx.setStorage({
                      key: 'orderlists',
                      data: arrs,
                    })
                    res.data = []
                    wx.setStorage({
                      key: 'carlists',
                      data: res.data,
                    })
                    wx.getStorage({
                      key: 'shopsnum',
                      success: function(res) {
                        res.data = []
                        wx.setStorage({
                          key: 'shopsnum',
                          data: res.data,
                        })
                      },
                    })
                  }
                })
              },
            })
          } else if (that.data.buynow == 1) {
            console.log(arrs)
            arrs[arrs.length - 1][0].addrname = that.data.choiceaddres.name
            arrs[arrs.length - 1][0].tel = that.data.choiceaddres.tel
            arrs[arrs.length - 1][0].city = that.data.choiceaddres.city
            arrs[arrs.length - 1][0].detail = that.data.choiceaddres.detailaddr
            wx.showModal({
              title: '确认支付',
              content: '请认真核对订单信息',
              confirmText: '确认支付',
              cancelText: '取消支付',
              success(res) {
                if (res.confirm) {
                  console.log(arrs)
                  arrs[arrs.length - 1][0].payinfo = 1
                  wx.showModal({
                    content: '支付成功',
                    confirmText: '查看订单',
                    confirmColor: '#e50329',
                    cancelText: '返回首页',
                    success(res) {
                      if (res.confirm) {
                        wx.navigateTo({
                          url: '../../pages/order/order',
                        })
                      } else if (res.cancel) {
                        wx.switchTab({
                          url: '../../pages/home/home',
                        })
                      }
                    }
                  })
                } else if (res.cancel) {
                  arrs[arrs.length - 1][0].payinfo = 0
                  wx.showModal({
                    content: '支付失败',
                    confirmText: '查看订单',
                    confirmColor: '#e50329',
                    cancelText: '返回首页',
                    success(r) {
                      if (r.confirm) {
                        wx.setStorage({
                          key: 'orderlists',
                          data: arrs,
                        })
                        wx.navigateTo({
                          url: '../../pages/order/order',
                        })
                      } else if (r.cancel) {
                        wx.switchTab({
                          url: '../../pages/home/home',
                        })
                      }
                    }
                  })
                }
                wx.setStorage({
                  key: 'orderlists',
                  data: arrs,
                })
                console.log(arrs)
              }
            })
          }
        }
      },
      fail: function() {
        wx.showModal({
          title: '提示',
          confirmText: '确定',
          cancelText: '取消',
          content: '请先设置地址',
          success: function(res) {
            if (res.confirm) {
              wx.redirectTo({
                url: '../../pages/addressList/addressList?chooseaddr=0&buynow=' + that.data.buynow + '&detail=' + JSON.stringify(that.data.nowbuydetail)
              })
            } else if (res.cancel) {
              return;
            }
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})